﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Json
{
    /// <summary>
    /// 使用Json方式映射对象
    /// </summary>
    public class JsonMapper
    {
        /// <summary>
        /// 映射对象
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TResult MapTo<TResult>(object source)
        {
            if (source == null)
            {
                return default;
            }
            var json = JsonConvert.SerializeObject(source);
            return JsonConvert.DeserializeObject<TResult>(json);
        }
    }
}
